package com.jxdinfo.hussar.cloud.module.example.dljc.basket.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.jxdinfo.hussar.cloud.module.example.dljc.basket.vo.BasketSearchVO;
import com.jxdinfo.hussar.cloud.module.example.dljc.basket.vo.BasketVO;
import com.jxdinfo.hussar.cloud.module.example.dljc.basketpoint.model.BasketPoint;
import com.jxdinfo.hussar.cloud.module.example.tjjc.util.DateUtil;
import com.jxdinfo.hussar.platform.core.base.apiresult.ApiResponse;
import com.jxdinfo.hussar.platform.core.utils.BeanUtil;
import com.jxdinfo.hussar.support.mp.base.service.impl.HussarServiceImpl;
import com.jxdinfo.hussar.support.datasource.annotations.HussarDs;
import com.jxdinfo.hussar.platform.core.utils.HussarUtils;
import com.jxdinfo.hussar.support.exception.HussarException;
import com.jxdinfo.hussar.cloud.module.example.dljc.basket.model.Basket;
import com.jxdinfo.hussar.cloud.module.example.dljc.basket.service.BasketService;
import com.jxdinfo.hussar.cloud.module.example.dljc.basket.dao.BasketMapper;
import dm.jdbc.util.StringUtil;
import org.apache.commons.lang3.ObjectUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Date;
import java.util.List;

/**
 * 吊篮表服务实现类
 */
@Service("dljc.basket.BasketServiceImpl")
@HussarDs("master")
public class BasketServiceImpl extends HussarServiceImpl<BasketMapper, Basket> implements BasketService {

    private static final Logger logger = LoggerFactory.getLogger(BasketServiceImpl.class);

    private static final String RETURN_CODE = "0";

    @Autowired
    private BasketMapper basketMapper;

    @Override
    public ApiResponse<List<BasketVO>> findByList(BasketSearchVO basketSearchVO) {
        QueryWrapper<Basket> wrapper = new QueryWrapper<>();
        if (StringUtil.isNotEmpty(basketSearchVO.getBasketName())) {
            wrapper.like("basket_name",basketSearchVO.getBasketName());
        }
        if (StringUtil.isNotEmpty(basketSearchVO.getTyProjectCode())) {
            wrapper.eq("ty_project_code",basketSearchVO.getTyProjectCode());
        }
        List<Basket> baskets = basketMapper.selectList(wrapper);
        List<BasketVO> basketVOS = BeanUtil.copyToList(baskets, BasketVO.class);
        return ApiResponse.success(basketVOS);
    }

    @Override
    public ApiResponse<BasketVO> findOne(BasketSearchVO basketSearchVO) {
        Basket basket = basketMapper.selectById(basketSearchVO.getId());
        BasketVO basketVO = BeanUtil.copyProperties(basket, BasketVO.class);
        return ApiResponse.success(basketVO);
    }

    @Override
    public ApiResponse<String> add(BasketSearchVO basketSearchVO) {
        QueryWrapper<Basket> wrapper = new QueryWrapper<>();
        wrapper.eq("basket_name", basketSearchVO.getBasketName());
        wrapper.eq("ty_delete_status", 0);
        Basket basket = basketMapper.selectOne(wrapper);
        if (ObjectUtils.isNotEmpty(basket)) {
            return ApiResponse.fail("添加失败，吊篮名称重复！");
        }

        QueryWrapper<Basket> wrapperBasket = new QueryWrapper<>();
        wrapperBasket.eq("device_code", basketSearchVO.getDeviceCode());
        wrapperBasket.eq("ty_delete_status", 0);
        Basket selectOne = basketMapper.selectOne(wrapperBasket);
        if (ObjectUtils.isNotEmpty(selectOne)) {
            return ApiResponse.fail("添加失败，设备已被添加！");
        }

        Basket basketSave = new Basket();
        BeanUtil.copyProperties(basketSearchVO, basketSave);
        basketSave.setTyCreateTime(DateUtil.toLocalDateTime(new Date()));
        basketSave.setTyCreateUserid("ContextUtil.getUserId()"); //todo
        basketSave.setTyCreateUserName("ContextUtil.getUserName()"); //todo


        basketMapper.insert(basketSave);

        return ApiResponse.success();
    }

    @Override
    public ApiResponse<String> updateBasket(BasketSearchVO basketSearchVO) {
        Basket basket = basketMapper.selectById(basketSearchVO.getId());
        if (ObjectUtils.isNotEmpty(basket)) {
            BeanUtils.copyProperties(basketSearchVO,basket);
            basket.setTyLastUpdateTime(DateUtil.toLocalDateTime(new Date()));
            basket.setTyLastUpdateUserid("ContextUtil.getUserId()"); //todo
            basket.setTyLastUpdateUserName("ContextUtil.getUserName()"); //todo

            basketMapper.updateById(basket);
        }
        return ApiResponse.success();
    }

    @Override
    public ApiResponse<String> deleteById(BasketSearchVO basketSearchVO) {
        Basket basket = basketMapper.selectById(basketSearchVO.getId());
        if (ObjectUtils.isNotEmpty(basket)) {
            BeanUtils.copyProperties(basketSearchVO,basket);
            basket.setTyDeleteStatus(1);
            basket.setTyLastUpdateTime(DateUtil.toLocalDateTime(new Date()));
            basket.setTyLastUpdateUserid("ContextUtil.getUserId()"); //todo
            basket.setTyLastUpdateUserName("ContextUtil.getUserName()"); //todo

            basketMapper.updateById(basket);
        }
        return ApiResponse.success();
    }
}
